

function getCookie(key){
	const cookie = document.cookie
	// a=100000; b=200; c=300
	const arr = cookie.split('; ')
	// 拿到的是一个数组 此时继续遍历循环数组 把数组里面的每一项进行切割
	for(let i = 0 ; i < arr.length ; i++){
		const arr1 = arr[i].split('=')
		// console.log(arr1)
		if(arr1[0] === key){
			return arr1[1]
		}
	}
	return null
}

function setCookie(key,value,expires){
	const date = new Date()
	if(expires){
		const time = date.getTime() - 8 * 60 * 60 * 1000 + expires * 1000
		date.setTime(time)
	}
	document.cookie = `${key}=${value};path=/;expires=${expires ? date : ''}`
}
function deleteCookie(key){
	setCookie(key,'随便',-1)
}
define(['template','request','jquery'],(template,req) => {

    class Header{
        constructor(){
            this.allCount =0
            this.init()
            
            // this.computedCount()
        }
        init(){
            $('header').load('/html/header.html',()=>{
                this.computedCount()
            })
            this.getNavData()
        }
        
        //获取数据 渲染导航栏
        getNavData(){
            // $.ajax({
            //     url:'/xmyx/api/tabs?sa=',
            //     dataType:'json'
            // })
            // .then(res => {
            //     // console.log(res)
            //     this.render(res.data.list)
            // })

            // console.log(req)
            req.getNavData()
                .then(res => {
                // console.log(res)
                    this.render(res.data.list)
                 })
                }
        //渲染页面
        render(list){
            list.shift()
        //    console.log(list)
        //    const html = template('navTemplate',{list})
        //    console.log(html)
           $('#navTabs').html(template('navTemplate',{list}))
        }
        //计算数量
        computedCount(){
            this.allCount = 0
            let prodList = localStorage.getItem('prodList')
            if(prodList){
                // console.log(prodList)
                prodList = JSON.parse(prodList)
                prodList.forEach(item=>{
                    this.allCount += item.count
                })
            }
            $('.choose span').text(this.allCount)
        }
    }
           
            
    return new Header()
})

